package com.dso.portal;


import javax.inject.Inject;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.MySQL5Dialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesViewResolver;
import com.dso.portal.core.database.hibernate.HibernateAccess;

@Configuration
@ComponentScan(basePackageClasses = AppConfig.class)
@EnableTransactionManagement
@EnableAspectJAutoProxy
@PropertySource({ "classpath:/jdbc.properties" })
public class AppConfig {

	private static final String TABLE_OPERATION = "update";

	@Inject
	Environment env;

	// @Bean
	// @Qualifier("messageSource")
	// public ResourceBundleMessageSource messageSource() {
	// ResourceBundleMessageSource bundleMessageSource = new
	// ResourceBundleMessageSource();
	// bundleMessageSource.setBasename("i18n.nstechs-i18n");
	// bundleMessageSource.setUseCodeAsDefaultMessage(true);
	// return bundleMessageSource;
	// }

	@Bean
	public SessionLocaleResolver localeResolver() {
		SessionLocaleResolver localeResolver = new SessionLocaleResolver();
		return localeResolver;
	}

	@Bean
	public TilesViewResolver viewResolver() {
		return new TilesViewResolver();
	}

	@Bean
	public TilesConfigurer tilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();
		tilesConfigurer
				.setDefinitions(new String[] { "classpath*:intranet-tiles.xml",
						"classpath*:website-tiles.xml" });
		return tilesConfigurer;
	}

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		return multipartResolver;
	}

	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(env
				.getRequiredProperty("jdbc.driverClassName"));
		dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
		dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
		dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
		dataSource.setTestOnBorrow(true);
		dataSource.setValidationQuery("select 1");
		return dataSource;
	}

	@Bean
	public SessionFactory sessionFactory() {
		LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(
				dataSource()).scanPackages(AppConfig.class.getPackage()
				.getName());
		builder.setProperty(org.hibernate.cfg.Environment.DIALECT,
				MySQL5Dialect.class.getName());
		builder.setProperty(org.hibernate.cfg.Environment.HBM2DDL_AUTO,
				TABLE_OPERATION);
		return builder.buildSessionFactory();
	}

	@Bean
	public HibernateAccess hibernateAccess() {
		HibernateAccess hibernateAccess = new HibernateAccess();
		hibernateAccess.setSessionFactory(sessionFactory());
		return hibernateAccess;
	}

    @Bean
    @Qualifier("business")
    public HibernateTransactionManager supplychainsTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory());
        return transactionManager;
    }
}